package cn.dnui_jzy913.dnuifood_16110100913.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import cn.dnui_jzy913.dnuifood_16110100913.R;
import cn.dnui_jzy913.dnuifood_16110100913.bean.UserBean;
import cn.dnui_jzy913.dnuifood_16110100913.common.Constants;
import cn.dnui_jzy913.dnuifood_16110100913.listener.RetrofitListener;
import cn.dnui_jzy913.dnuifood_16110100913.model.UserModel;

/**
 * Created by Administrator on 2018/10/15.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener ,RetrofitListener<UserBean> {
    SharedPreferences sp;
    private EditText username,userpass;
    private Button login,register;

    String user_id;

    @Override
    protected int initlayout() {
        return R.layout.activity_login;
    }
    @Override
    void initViews() {
        username=findViewById(R.id.editText);
        userpass=findViewById(R.id.editText2);
        login=findViewById(R.id.button);
        register=findViewById(R.id.button2);
    }

    @Override
    void initEvents() {
        sp=getSharedPreferences("userInfo",Constants.MODE_PRIVATE);
        username.setText(sp.getString("username",""));
        userpass.setText(sp.getString("userpass",""));
        login.setOnClickListener(this);
        register.setOnClickListener(this);
    }

    @Override
    void initData() {
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button://登录
                login();
                break;
            case R.id.button2://注册
                Bundle bundle = new Bundle();
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                intent.putExtras(bundle);
                startActivityForResult(intent,1);
                break;

        }
    }
    @Override
    protected void onActivityResult (int requestCode, int resultCode, Intent data) {//requestCode请求码，resultCode结果码
        switch(requestCode){
            case 1:
                //来自按钮login的请求，作相应业务处理
                super.onActivityResult(requestCode,resultCode,data);
                username.setText(data.getStringExtra("username"));
                userpass.setText(data.getStringExtra("userpass"));
            case 2:

        }
    }
    @Override
    public void onSuccess(UserBean userBean, int flag) {
        //处理网络请求成功的结果
        if (userBean.getUserid().equals("0")){
            showToast("用户名或者密码错误");
        }else {
            user_id = userBean.getUserid();
            SaveUser(userBean.getUserid());//保存登录用户信息

            Intent intent=new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);
            showToast("登陆成功");
            finish();
        }
    }

    @Override
    public void onFalure() {
        showToast("网络解析错误");
    }

    public void SaveUser(String user_id){
        //保存登录用户信息
        sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();//获取编辑器
        editor.putString("username",username.getText().toString());
        editor.putString("user_id",user_id);
        editor.putString("userpass",userpass.getText().toString());
        editor.commit();//提交修改

    }
    public void login(){
        String uname=username.getText().toString().trim();
        String upass=userpass.getText().toString().trim();
        UserModel userModel = new UserModel();
        userModel.login(uname,upass,this);
    }

}
